﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类WorkTaskPower。
    /// </summary>
    public class WorkTaskPower
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "Powerid", "PowerName", "Status", "WorkFlowId", "WorkTaskId" };
        private string tableName = "HF_WorkTaskPower";
        private string keyField = "PowerId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkTaskPowerEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@Powerid", entity.PowerId, typeof(string));
            sqlDataItem.AppendParameter("@PowerName", entity.PowerName, typeof(string));
            sqlDataItem.AppendParameter("@Status", entity.Status, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@WorkTaskId", entity.WorkTaskId, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public WorkTaskPowerEntity GetWorkTaskPowerEntity(DataRow dr)
        {
            WorkTaskPowerEntity entity = new WorkTaskPowerEntity();
            entity.PowerId = dr["Powerid"].ToString();
            entity.PowerName = dr["PowerName"].ToString();
            entity.Status = dr["Status"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.WorkTaskId = dr["WorkTaskId"].ToString();
            return entity;
        }

        #endregion
        #region  构造函数
        public WorkTaskPower()
        { }
        public static WorkTaskPower GetInstance()
        {
            return new WorkTaskPower();
        }
        #endregion
        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkTaskPowerEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkTaskPowerEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string powerid)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, powerid);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="powerid">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string powerid)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, powerid);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="Powerid">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkTaskPowerTable(string powerid)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, powerid);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="powerid">主键Id</param>
        /// <returns>DataTable</returns>
        public WorkTaskPowerEntity GetWorkTaskPowerEntity(string powerid)
        {
            DataTable dt = GetWorkTaskPowerTable(powerid);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetWorkTaskPowerEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion
        #region  扩展方法
        //此处添加扩展方法

        /// <summary>
        /// 获得任务节点的控制权限
        /// </summary>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns></returns>
        public  DataTable GetTaskPowers(string workTaskId)
        {
            try
            {
                string sqlStr = "select * from HF_WorkTaskPower where WorkTaskId =@workTaskId ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点的控制权限
        /// </summary>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns>权限码，多个权限用逗号隔开。</returns>
        public string GetTaskPowerStr(string workTaskId)
        {
            string result = "";
            try
            {
                DataTable dt = GetTaskPowers(workTaskId);
                foreach (DataRow dr in dt.Rows)
                {
                    result = result + dr["PowerName"].ToString() + ",";
                }
                return result.TrimEnd('，');
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 设置任务节点的控制权限
        /// </summary>
        /// <param name="powerName">权限名</param>
        /// <param name="workflowid">workflowid</param>
        /// <param name="worktaskId">worktaskId</param>
        public bool SetTaskPower(string powerName, string workFlowId, string workTaskId)
        {
            try
            {
                WorkTaskPowerEntity entity = new WorkTaskPowerEntity();
                entity.PowerId = Guid.NewGuid().ToString();
                entity.WorkFlowId = workFlowId;
                entity.WorkTaskId = workTaskId;
                entity.PowerName = powerName;
                entity.Status = "1";
                Insert(entity);
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

